package demo.practice.competition;

import java.util.Arrays;
import java.util.PriorityQueue;

class Kv{
    int k;
    int v;

    public Kv(int k, int v) {
        this.k = k;
        this.v = v;
    }

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }

    public int getV() {
        return v;
    }

    public void setV(int v) {
        this.v = v;
    }
}

public class P5641 {

    public static void main(String[] args) {
        P5641 p5641= new P5641();
        p5641.maximumUnits(new int[][]{{1,3},{2,2},{3,1}},  4);
    }
    public int maximumUnits(int[][] boxTypes, int truckSize) {

        int[] index2s= new int[boxTypes.length];

        PriorityQueue<Kv>priorityQueue= new PriorityQueue<Kv>(
               (o1, o2) -> o2.getV()-o1.getV()//k为个数，v为大小
        );



        for(int i=0;i<boxTypes.length;i++){
            priorityQueue.add(new Kv(boxTypes[i][0],boxTypes[i][1]));
        }

        //Arrays.sort(index2s)
        int sum=0;
        while (truckSize>0&&priorityQueue.size()>0){
            Kv poll = priorityQueue.poll();
            if(poll.getK()<=truckSize){
                //
                sum+=poll.getK()*poll.getV();
                truckSize=truckSize-poll.getK();
            }else{
                sum+=truckSize*poll.getV();
                break;
            }
        }
        return sum;
    }
}
